/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SetupWizardFrame.java
 *
 * Created on May 31, 2010, 10:24:50 PM
 */

package yayman;

import org.jdesktop.application.Action;
import java.io.*;
import javax.swing.*;
import javax.swing.DefaultComboBoxModel;
import java.util.*;
import java.util.logging.*;
import static com.moviejukebox.tools.PropertiesUtil.setProperty;

/**
 *
 * @author Dan
 */
public class SetupWizardBasicFrame extends javax.swing.JDialog {
    private EditPropertiesFrame propsFrame;
    //private EditLibraryFrame libFrame;
    private YAYManPrefs prefsWindow;
    private SavedJukebox jukebox;
    static Logger logger = Logger.getLogger("yayman");
    private String propsFile;
    private String libFile;

    public SetupWizardBasicFrame(YAYManPrefs parent) {
        super(parent.getMainFrame().getFrame(), true);
        initComponents();

        propsFile = "moviejukebox.properties";
        libFile = "My_Library.xml";

        prefsWindow = parent;
        jukebox = new SavedJukebox();
        jukebox.setPropertiesFile(propsFile);
        jukebox.setLibraryFile(libFile);
        setProperty("yayman.propertiesFile", propsFile);
        jukebox.setLibraryFile(libFile);
        propsFrame = new EditPropertiesFrame(prefsWindow, jukebox, false, EditPropertiesFrame.PropertiesMode);

        DefaultComboBoxModel model = new DefaultComboBoxModel();
        //model.addElement(new NetworkedMediaTank("None"));
        PopAPI.syncNmtsWith(model);
        nmtList.setModel(model);

        checkJukeboxDir();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        jukeboxRootPop = new javax.swing.JPopupMenu();
        jukeboxRootChooseMenu = new javax.swing.JMenuItem();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        nmtList = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtJukeboxRoot = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        btnFinish = new javax.swing.JButton();

        fileChooser.setName("fileChooser"); // NOI18N

        jukeboxRootPop.setName("jukeboxRootPop"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(yayman.YAYManApp.class).getContext().getActionMap(SetupWizardBasicFrame.class, this);
        jukeboxRootChooseMenu.setAction(actionMap.get("chooseJukeboxRoot")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(yayman.YAYManApp.class).getContext().getResourceMap(SetupWizardBasicFrame.class);
        jukeboxRootChooseMenu.setIcon(resourceMap.getIcon("jukeboxRootChooseMenu.icon")); // NOI18N
        jukeboxRootChooseMenu.setText(resourceMap.getString("jukeboxRootChooseMenu.text")); // NOI18N
        jukeboxRootChooseMenu.setName("jukeboxRootChooseMenu"); // NOI18N
        jukeboxRootPop.add(jukeboxRootChooseMenu);

        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel12.setFont(resourceMap.getFont("jLabel12.font")); // NOI18N
        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N

        nmtList.setAction(actionMap.get("selectNMTAction")); // NOI18N
        nmtList.setName("nmtList"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jButton1.setAction(actionMap.get("editLibFile")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txtJukeboxRoot.setEditable(false);
        txtJukeboxRoot.setText(resourceMap.getString("txtJukeboxRoot.text")); // NOI18N
        txtJukeboxRoot.setComponentPopupMenu(jukeboxRootPop);
        txtJukeboxRoot.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        txtJukeboxRoot.setName("txtJukeboxRoot"); // NOI18N
        txtJukeboxRoot.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtJukeboxRootMouseClicked(evt);
            }
        });

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        btnFinish.setAction(actionMap.get("finishWizard")); // NOI18N
        btnFinish.setText(resourceMap.getString("btnFinish.text")); // NOI18N
        btnFinish.setName("btnFinish"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nmtList, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 214, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addContainerGap(87, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtJukeboxRoot, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(12, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addComponent(btnFinish)
                .addContainerGap(134, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nmtList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addGap(11, 11, 11)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtJukeboxRoot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnFinish)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtJukeboxRootMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtJukeboxRootMouseClicked
        txtJukeboxRoot.getComponentPopupMenu().show(txtJukeboxRoot, evt.getX(), evt.getY());
    }//GEN-LAST:event_txtJukeboxRootMouseClicked

    public SavedJukebox getJukebox() {
        return jukebox;
    }

    @Action
    public void editLibFile() {
        EditLibraryFrame editLib = new EditLibraryFrame(prefsWindow.getMainFrame().getFrame(), jukebox, this);
        editLib.setLocationRelativeTo(this);
        editLib.setVisible(true);
    }

    @Action
    public void chooseJukeboxRoot() {
        if (!txtJukeboxRoot.getText().isEmpty()) {
            fileChooser.setCurrentDirectory(new File(txtJukeboxRoot.getText()));
        } else {
            fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
        }
        fileChooser.setSelectedFile(new File(txtJukeboxRoot.getText()));
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setDialogTitle("Choose jukebox output folder");
        if (fileChooser.showOpenDialog(txtJukeboxRoot) == JFileChooser.APPROVE_OPTION) {
            txtJukeboxRoot.setText(fileChooser.getSelectedFile().getPath().replace("\\", "/"));
        }
        checkJukeboxDir();
    }

    public void checkJukeboxDir() {
        if (txtJukeboxRoot.getText() == null || txtJukeboxRoot.getText().isEmpty()) {
            btnFinish.setEnabled(false);
        } else {
            btnFinish.setEnabled(true);
        }
    }

    @Action
    public void finishWizard() {
        propsFrame.setProperty("mjb.jukeboxRoot", txtJukeboxRoot.getText());
        propsFrame.closeSave();
        prefsWindow.addJukebox(jukebox);
        this.setVisible(false);
        this.dispose();
    }

    @Action
    public void selectNMTAction() {
        if (nmtList.getSelectedIndex() > 0) {
            jukebox.setNmt((NetworkedMediaTank)nmtList.getSelectedItem());
        } else {
            jukebox.setNmt(null);
        }
    }

    public void updateLibraryStatus() {
        EditLibraryFrame libFrame = new EditLibraryFrame(prefsWindow.getMainFrame().getFrame(), true);
        File file = new File(libFile);
        if (!file.exists()) {
            btnFinish.setEnabled(false);
        } else if (!libFrame.isValidLibraryFile(file)) {
            btnFinish.setEnabled(false);
        } else {
            btnFinish.setEnabled(true);
            if (txtJukeboxRoot.getText().isEmpty()) {
                List<NMTLibrary> libs = libFrame.getLibraries();
                for (NMTLibrary lib : libs) {
                    txtJukeboxRoot.setText(lib.getPath());
                    checkJukeboxDir();
                    break;
                }
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFinish;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JMenuItem jukeboxRootChooseMenu;
    private javax.swing.JPopupMenu jukeboxRootPop;
    private javax.swing.JComboBox nmtList;
    private javax.swing.JTextField txtJukeboxRoot;
    // End of variables declaration//GEN-END:variables

}
